﻿using System;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;

namespace Practical.SharePoint.Base
{
    public class BaseAjaxUserControl : UserControl
    {
        protected string serviceUrlName = "_" + Guid.NewGuid().ToString("N");
        protected string serviceUrl = "/_vti_bin/ListData.svc";

        protected virtual void Page_Load(object sender, EventArgs e)
        {
            ClientScriptManager cs = Page.ClientScript;
            Type csType = Page.GetType();
            if (!cs.IsClientScriptBlockRegistered(csType, serviceUrlName))
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("var {0} = '{1}/{2}';", serviceUrlName, SPContext.Current.Web.Url.TrimEnd('/'), serviceUrl);
                cs.RegisterClientScriptBlock(csType, serviceUrlName, sb.ToString(), true);
            }
        }
    }
}
